﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TainguyenComponent.BLL;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace TainguyenComponent.DAL
{
    public class  TainguyenDAL: ITainguyenDAL

    {
        #region ITainguyen Connection
        private string connectionstring = Commom.WCManager.ConnectionString();

        public string Connection
        {
            get { return connectionstring; }
            set { connectionstring = value; }
        }

        #endregion

        #region ITainguyen members
        #region Get
        public void Get(Tainguyen tainguyen)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(this.Connection))
                {
                    SqlParameter[] par = new SqlParameter[1];
                    par[0] = new SqlParameter("@tai_nguyen_id", tainguyen.Tai_nguyen_id);
                    using (SqlDataReader dr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, "P_tai_nguyen_Get", par))
                    {
                        while (dr.Read())
                        {
                            tainguyen.Tai_nguyen_id = int.Parse(dr["tai_nguyen_id"].ToString());
                            tainguyen.Ung_dung_id = int.Parse(dr["ung_dung_id"].ToString());
                            tainguyen.Ma=dr["ma"].ToString();
                            tainguyen.Ten=dr["ten"].ToString();
                            tainguyen.Path=dr["path"].ToString();
                            tainguyen.Tenfile=dr["tenfile"].ToString();
                            tainguyen.Url = dr["url"].ToString();
                            tainguyen.Type = int.Parse(dr["type"].ToString());

                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        #endregion

        #region Add
        public int Add(Tainguyen tainguyen)
        {
            int rowNo = -1;
            try
            {
                using (SqlConnection conn = new SqlConnection(this.Connection))
                {
                    SqlParameter[] par = new SqlParameter[8];
                    par[0] = new SqlParameter("@tai_nguyen_id", tainguyen.Tai_nguyen_id);
                    par[0].Direction = ParameterDirection.Output;
                    par[1] = new SqlParameter("@ung_dung_id", tainguyen.Ung_dung_id);
                    par[2] = new SqlParameter("@ma", tainguyen.Ma);
                    par[3] = new SqlParameter("@ten", tainguyen.Ten);
                    par[4] = new SqlParameter("@path", tainguyen.Path);
                    par[5] = new SqlParameter("@tenfile", tainguyen.Tenfile);
                    par[6] = new SqlParameter("@url", tainguyen.Url);
                    par[7] = new SqlParameter("@type", tainguyen.Type);
                    SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "P_tai_nguyen_Insert", par);
                    rowNo = int.Parse(par[0].Value.ToString());
                }

            }
            catch (Exception ex)
            {
            }
            return rowNo;
        }
        #endregion

        #region Delete
        public void Delete(Tainguyen tainguyen)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(this.Connection))
                {
                    SqlParameter[] par = new SqlParameter[1];
                    par[0] = new SqlParameter("@tai_nguyen_id", tainguyen.Tai_nguyen_id);
                    SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "P_tai_nguyen_Delete", par);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        #endregion

        #region Update
        public void Update(Tainguyen tainguyen)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(this.Connection))
                {
                    SqlParameter[] par = new SqlParameter[8];
                    par[0] = new SqlParameter("@tai_nguyen_id", tainguyen.Tai_nguyen_id);
                    par[0].Direction = ParameterDirection.Output;
                    par[1] = new SqlParameter("@ung_dung_id", tainguyen.Ung_dung_id);
                    par[2] = new SqlParameter("@ma", tainguyen.Ma);
                    par[3] = new SqlParameter("@ten", tainguyen.Ten);
                    par[4] = new SqlParameter("@path", tainguyen.Path);
                    par[5] = new SqlParameter("@tenfile", tainguyen.Tenfile);
                    par[6] = new SqlParameter("@url", tainguyen.Url);
                    par[7] = new SqlParameter("@type", tainguyen.Type);
                    SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, "P_tai_nguyen_Update", par);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        #endregion

        #region GetAll
        public List<Tainguyen> GetAll(int currentPage, int pageSize, out int total, string ten, string column_sort, string type_sort)
        {
            try
            {
                List<Tainguyen> soList = new List<Tainguyen>();
                using (SqlConnection con = new SqlConnection(this.Connection))
                {
                    SqlParameter[] par = new SqlParameter[6];
                    par[0] = new SqlParameter("@currentpage", currentPage);
                    par[1] = new SqlParameter("@pagesize", pageSize);
                    par[2] = new SqlParameter("@total", SqlDbType.Int);
                    par[2].Direction = ParameterDirection.Output;
                    par[3] = new SqlParameter("@solution_name", ten);
                    par[4] = new SqlParameter("@column_sort", column_sort);
                    par[5] = new SqlParameter("@type_sort", type_sort);

                    using (SqlDataReader dr = SqlHelper.ExecuteReader(con, CommandType.StoredProcedure, "P_tai_nguyen_GetAll", par))
                    {
                        while (dr.Read())
                        {
                            Tainguyen tainguyen = new Tainguyen();
                            tainguyen.Tai_nguyen_id = int.Parse(dr["tai_nguyen_id"].ToString());
                            tainguyen.Ung_dung_id = int.Parse(dr["ung_dung_id"].ToString());
                            tainguyen.Ma = dr["ma"].ToString();
                            tainguyen.Ten = dr["ten"].ToString();
                            tainguyen.Path = dr["path"].ToString();
                            tainguyen.Tenfile = dr["tenfile"].ToString();
                            tainguyen.Url = dr["url"].ToString();
                            tainguyen.Type = int.Parse(dr["type"].ToString());
                        }
                    }
                    total = int.Parse(par[2].Value.ToString());
                }
                return soList;
            }
            catch (Exception ex) { throw; }
        }
        #endregion
        #endregion
    }
}
